package com.yh.homestay.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.homestay.common.HomeStayResult;
import com.yh.homestay.dao.LogListMapper;
import com.yh.homestay.dao.SysLogMapper;
import com.yh.homestay.pojo.CapitalGoods;
import com.yh.homestay.pojo.SysLog;
import com.yh.homestay.service.LogModelService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LogModelServiceImpl implements LogModelService {

    private Log logger= LogFactory.getLog(LogModelServiceImpl.class);

    @Autowired
    private SysLogMapper sysLogMapper;

    @Autowired
    private LogListMapper logListMapper;

    @Override
    public HomeStayResult insertLog(SysLog sysLog) {
        try{
            sysLogMapper.insert(sysLog);
            return HomeStayResult.ok();
        }catch (Exception e){
            logger.debug(e);
            return HomeStayResult.build(500,"error");
        }
    }

    /**
    * @Date: 2020/1/13 16:44
    * @Description: 查询数据库中的日志操作
    * @param page
     * @param limit
     * @param start
     * @param end
     * @param logAccessAccount 
    * @return: com.yh.homestay.common.HomeStayResultG
    */ 
    @Override
    public HomeStayResult showLogs(Integer page, Integer limit, String start, String end, String logAccessAccount) {
        try {
            PageHelper.startPage(page, limit);
            List<SysLog> sysLogs;
            if (StringUtils.isEmpty(logAccessAccount)) {
                sysLogs = sysLogMapper.selectAll();
            } else {
                Example example = new Example(SysLog.class);
                example.createCriteria().andLike("logAccessAccount", "%" + logAccessAccount + "%");
                sysLogs = sysLogMapper.selectByExample(example);
            }
            PageInfo info = new PageInfo(sysLogs);
            return new HomeStayResult(0, "ok", (int) info.getTotal(), info.getList());
        } catch (Exception e) {
            logger.debug(e);
            return HomeStayResult.build(500, "error");
        }
    }

    /**
    * @Date: 2020/1/13 16:45
    * @Description: 查询一个日志信息
    * @param num 
    * @return: com.yh.homestay.common.HomeStayResultG
    */ 
    @Override
    public HomeStayResult showOneLog(Integer num) {
       try{
           SysLog sysLog = sysLogMapper.selectOneByExample(num);
           return HomeStayResult.ok(200,sysLog);
       }catch (Exception e){
           logger.debug(e);
           return HomeStayResult.build(500,"error");
       }
    }


}
